﻿<?php
	if(!$_GET['id'] || !$_GET['board'])
		echo '<meta http-equiv="refresh" content="0; url=/errors/404.html"/>';

	require_once('fw/sqllib.php');
	
	$id = htmlentities(mysql_escape_string($_GET['id']));
	$board = htmlentities(mysql_escape_string($_GET['board']));

	if(!$board)
		echo '<meta http-equiv="refresh" content="0; url=/mainpage.php"/>';

	//fetching boards
	$db->select('boards', null, array( 'hidden' => '=0' ));
	$boards = $db->query();

	$db->select('posts', null, array(
		'thread_id' => '='.$id.' AND',
		'board'     => "='$board'"
	), 'date asc');

	$posts = $db->query();
	
	$postcount = mysql_affected_rows();

	$db->select('posts', null, array(
		'id'    => '='.$id.' AND',
		'board' => "='$board'"
	));
	$oppost = $db->query();
	$oppost = $oppost[0];

	if(!$oppost)
		echo '<meta http-equiv="refresh" content="0; url=/errors/404.html"/>';
?>


<!DOCTYPE HTML>
<html>
	<head>
		<meta http-equiv="content-type" 
				content="text/html;charset=utf-8" />
		<title>BORDACHKA 0.01</title>
		<link rel="stylesheet" src="style.css"/>
	</head>
	<body>
		<h1><a href="/<?=$board?>">БОРДА-БОРДАЧКА</a></h1>

		<div class='boardlist'>
			<?php foreach($boards as $brd): ?>
				/<a href="/<?=$brd['short']?>" title="<?=$brd['name']?>">
					<?=$brd['short'];?>
				</a>/
			<?php endforeach; ?>
		</div>

		<form class='postarea' action="/post.php" method="post" enctype="multipart/form-data">
			<input type="hidden" name="thread_id" value="<?=$id?>"/>
			<input type="hidden" name="board" value="<?=$board?>"/>
			<p>Тема: <input name="title" /></p>
			<p>Имя: <input name="name" /></p>
			<p>Текст: <p><textarea name="text"></textarea></p></p>
			<p>Изображение: <input type="file" name="picture" /></p>
			<button>Отправить</button>
		</form>
		
		<hr/>

		<div class='oppost'>
			<p class='header'>
				<span class='id'>
					#<?=$oppost['id']?>
				</span>
				<span class='title'>
					<?=$oppost['title']?>
				</span>
				<span class='name'>
					<?=$oppost['name']?>
				</span>
				<span class='date'>
					<?=$oppost['date']?>
				</span>
				<span class='answers'>
					Ответов: <?=$postcount?>
				</span>
			</p>

			<?if($oppost['pic']):?>
				<?php
					$oppost['thumb'] = explode('.',$oppost['pic']);
					$oppost['thumb'] = $oppost['thumb'][0].'.png';
				?>

				<a href="/pics/<?=$oppost['pic']?>">
					<img src="/pics/s/<?=$oppost['thumb']?>"/>
				</a>
			<?endif;?>

			<p class='text'>
				<?=$oppost['text']?>
			</p>
		</div>
		<hr/>
		
		<?foreach($posts as $post):?>
			<div class='post' id="<?=$id?>">
				<p class='header'>
					<span class='postid'>
						#<?=$post['id']?>
					</span>
					<span class='title'>
						<?=$post['title']?>
					</span>
					<span class='name'>
						<?=$post['name']?>
					</span>
					<span class='date'>
						<?=$post['date']?>
					</span>
				</p>

				<?if($post['pic']):?>
					<?php
						$post['thumb'] = explode('.',$post['pic']);
						$post['thumb'] = $post['thumb'][0].'.png';
					?>

					<a href="/pics/<?=$post['pic']?>">
						<img src="/pics/s/<?=$post['thumb']?>"/>
					</a>
				<?endif;?>

				<p class='text'>
					<?=$post['text']?>
				</p>
			</div>
		<?endforeach;?>
	</body>
</html> 
